package com.ruoyi.web.controller.buy;

import com.ruoyi.buy.domain.entity.PO.BuySpaPricePO;
import com.ruoyi.buy.domain.entity.buy0001.BuyDrawingImportData;
import com.ruoyi.buy.domain.entity.buy0001.BuySpa;
import com.ruoyi.buy.domain.entity.buy0001.BuySpaImportData;
import com.ruoyi.buy.domain.entity.buy0001.BuySpaSearchInDto;
import com.ruoyi.buy.service.IBuy0001Service;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色信息
 * 
 * @author ruoyi
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping("/buy/buy0001")
public class buySpaListController extends BaseController
{
    @Autowired
    private IBuy0001Service buy0001Service;

    @GetMapping("/list")
    public TableDataInfo list(BuySpaSearchInDto buySpaSearchInDto)
    {
        startPage();
        List<BuySpa> list = buy0001Service.selectSpaList(buySpaSearchInDto);
        return getDataTable(list);
    }

    /**
     * 删除字典类型
     */
    @Log(title = "零件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{iDs}")
    public AjaxResult remove(@PathVariable Long[] iDs)
    {
        buy0001Service.deleteDictDataByIds(iDs);
        return success();
    }

    /**
     * 新增零件信息
     */
    @Log(title = "零件信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BuySpa param) throws Exception {
        param.setCreateBy(getUsername());
        int firstFlag = this.buy0001Service.insertSpaInfo(param);
        return toAjax(firstFlag);
    }

    /**
     * 查询零件数据详细
     */
    @PostMapping(value = "/selectById")
    public AjaxResult getInfo(@RequestBody BuySpa param) throws Exception {
        return success(buy0001Service.selectSpaById((param.getiD())));
    }

    /**
     * 修改零件信息
     */
    @Log(title = "零件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BuySpa param) throws Exception {
        param.setUpdateBy(getUsername());
        int firstFlag = this.buy0001Service.updateSpaInfo(param);
        return toAjax(firstFlag);
    }

    /**
     * 修改零件信息
     */
    @PostMapping("/viewPrice")
    public AjaxResult viewPrice(@RequestBody BuySpa param) throws Exception {
        param.setUpdateBy(getUsername());
        List<BuySpaPricePO> list= this.buy0001Service.viewPrice(param);
        return success(list);
    }

    @Log(title = "物料名录", businessType = BusinessType.IMPORT)
    @PostMapping("/importSpaData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<BuySpaImportData> util = new ExcelUtil<>(BuySpaImportData.class);
        List<BuySpaImportData> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        this.buy0001Service.importSpaData(list,operName);
//        String message = userService.importUser(userList, updateSupport, operName);
        return success(1);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BuySpaImportData> util = new ExcelUtil<BuySpaImportData>(BuySpaImportData.class);
        util.importTemplateExcel(response, "物料价格数据导入模板");
    }

    @Log(title = "导入投产表", businessType = BusinessType.IMPORT)
    @PostMapping("/importDrawData")
    public AjaxResult importDrawData(MultipartFile file) throws Exception
    {
        ExcelUtil<BuyDrawingImportData> util = new ExcelUtil<>(BuyDrawingImportData.class);
        List<BuyDrawingImportData> list = util.importExcel(file.getInputStream(),1);

        for (BuyDrawingImportData item:list
             ) {
            if(StringUtils.isEmpty(item.getVSpaName())){
                throw new Exception("存在图纸名称为空的数据，请确认");
            }else if(StringUtils.isEmpty(item.getVSpaModel())){
                throw new Exception("存在材料为空的数据，请确认");
            }else if(StringUtils.isEmpty(item.getVSpaBrand())){
                throw new Exception("存在厂家为空的数据，请确认");
            }
            item.setvKind("04");
        }
        String operName = getUsername();
        this.buy0001Service.importDrawData(list,operName);
        return success(1);
    }
}
